/*
 * NuevoRol.java
 *
 * Created on Oct 31, 2008, 6:41:14 PM
 */
package wasolicitudesfiec.Administracion.Roles;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Button;
import com.sun.rave.web.ui.component.ImageHyperlink;
import com.sun.rave.web.ui.component.TextField;
import com.sun.rave.web.ui.model.DefaultTableDataProvider;
import entidad.Rol;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.validator.LengthValidator;
import wasolicitudesfiec.SessionBean1;
import wasolicitudesfiec.RequestBean1;
import wasolicitudesfiec.ApplicationBean1;
import wasolicitudesfiec.Menu;
import wasolicitudesfiec.dataproviders.administracion.Dp_Persona;
import wasolicitudesfiec.logicacontrol.Logica;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @author kabad
 */
public class NuevoRol extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private LengthValidator lengthValidator1 = new LengthValidator();

    public LengthValidator getLengthValidator1() {
        return lengthValidator1;
    }

    public void setLengthValidator1(LengthValidator lv) {
        this.lengthValidator1 = lv;
    }

    // </editor-fold>
    private Dp_Persona dp_Persona = new Dp_Persona();
    private int id;
    private Logica logica;
    private TextField txtNombre = new TextField();

    public TextField getTxtNombre() {
        return txtNombre;
    }

    public void setTxtNombre(TextField tf) {
        this.txtNombre = tf;
    }
    private TextField txtDescripcion = new TextField();

    public TextField getTxtDescripcion() {
        return txtDescripcion;
    }

    public void setTxtDescripcion(TextField tf) {
        this.txtDescripcion = tf;
    }
    private Button btnCrear = new Button();

    public Button getBtnCrear() {
        return btnCrear;
    }

    public void setBtnCrear(Button b) {
        this.btnCrear = b;
    }

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public NuevoRol() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("NuevoRol Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        // </editor-fold>
        logica = new Logica();
        this.getMenu().setTitulo("Administración -> Roles -> Nuevo rol");
    }

    @Override
    public void preprocess() {
    }

    @Override
    public void prerender() {
    }

    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public Dp_Persona getDp_Persona() {
        return dp_Persona;
    }

    public void setDp_Persona(Dp_Persona dp_Persona) {
        this.dp_Persona = dp_Persona;
    }

    public String imlinkAgregarUsuario_action() {
        // TODO: qui lo mismo que en modificar rol cuando a este listoa
        return null;
    }

    public String imlinkRemover_action() {
        // TODO: Replace with your code
        return null;
    }

     public Menu getMenu() {
        return (Menu) getBean("SolicitudesFIEC$Menu");
    }
    public String btnCrear_action() {
        FacesMessage message;
        try {
            Rol rol = new Rol();
            rol.setDescripcion((String) getTxtDescripcion().getText());
            rol.setRol((String) getTxtNombre().getText());
            Vector<Rol> roles = logica.leerRoles();
            boolean existeRol = false;
            for (Rol itemRol : roles) {
                String nuevorol=rol.getRol().toLowerCase();
                String rolexistente= itemRol.getRol().toLowerCase();
                if (nuevorol.equals(rolexistente)) {
                    existeRol = true;
                    break;
                }
            }
            if (existeRol) {
                message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "El rol específicado ya existe", "");
                getFacesContext().addMessage(null, message);
                return null;
            } else {
                logica.nuevoRolMiembros(rol, this.getDp_Persona().getListaRg_Persona());
//                logica.addCookieMensaje("La operación fue exitosa", this.getExternalContext());
                  return "roles";
            }
          
        } catch (Exception ex) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ocurrió un error no esperado, comuníquese con el administrador del sistema", ex.getLocalizedMessage());
            getFacesContext().addMessage(null, message);
            Logger.getLogger(NuevoRol.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public String persona_id_btn_action() {
        // TODO: Process the button click action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }
}

